package jwdt.xadmin.rskq.service.impl;

import java.util.List;

import jwdt.common.dao.BaseDao;
import jwdt.xadmin.rskq.bean.TKqwc;
import jwdt.xadmin.rskq.service.KqwcManageService;
import jwdt.xadmin.system.bean.TUser;

import com.ezerg.db.Jdb;
import com.ezerg.db.ext.PageUtil;
import com.ezerg.util.DateUtil;
import com.ezerg.util.StringUtil;
import com.ezerg.web.Form;
import com.ezerg.web.Module;

public class KqwcManageServiceImpl implements KqwcManageService {

	private BaseDao<TKqwc> baseDao;
	
	public void setBaseDao(BaseDao<TKqwc> baseDao) {
		this.baseDao = baseDao;
	}
	
	@Override
	public PageUtil doList(PageUtil pu, TKqwc tc ,String flag) throws Exception{
		String where = "";
		String odatetime=tc.getOdatetime();
		if(!StringUtil.nullOrBlank(odatetime)) {
			where += "odatetime BETWEEN '"+odatetime+" 00:00:00' AND '"+odatetime+" 23:59:59'";
		}
		if(!StringUtil.nullOrBlank(flag)) {
			if(!StringUtil.nullOrBlank(odatetime)){where+=" and";}
			where += " flag='"+flag+"'";
		}
		String sql = " order by id desc";
		pu.doList(TKqwc.class,where+sql);
		//创造json数据对象
		return pu;
	}
	
	@Override
	public List kqwc_check(String flag) throws Exception{
		return Jdb.get().query(
				"SELECT * FROM (SELECT * FROM t_kq_wcsq a LEFT JOIN (SELECT fullname AS xleader,UserId FROM t_user ) b ON a.applicant=b.UserId) a LEFT JOIN (SELECT fullname AS xapplicant,UserId FROM t_user ) b ON a.leader=b.UserId where flag='" + flag + "'");
	}
	
	@Override
	public String kqwcaddact(TKqwc obj,TUser userInfo) throws Exception{
		obj.setOdatetime(DateUtil.getDateTime());
		obj.setFlag("0");
		obj.setApplicant(userInfo.getUserid());
		return baseDao.doAddObj(obj);
	}
	
	@Override
	public TKqwc kqwcedit(int id) throws Exception{
		return baseDao.doLoadObj(TKqwc.class,id);
	}
	
	@Override
	public String kqwcEditact(TKqwc obj,int id) throws Exception{
		return baseDao.doUpdateObj(TKqwc.class,id,obj);
	}
	@Override
	public String kqwcDel(int id) throws Exception{
		return baseDao.doDelObj(TKqwc.class,id);
	}
	
	@Override
	public String kqwcSpb(Form f, Module m,int id) throws Exception{
		TKqwc obj = (TKqwc) Jdb.get().get(TKqwc.class, id);
		obj.setFlag("2");
		obj = (TKqwc) f.copy2Obj(obj);
		return baseDao.doUpdateObj(TKqwc.class,id,obj);
	}
	
	@Override
	public String kqwcSpt(Form f, Module m,int id) throws Exception{
		
		TKqwc obj = (TKqwc) Jdb.get().get(TKqwc.class, id);
		obj.setFlag("1");
		obj = (TKqwc) f.copy2Obj(obj);
		return baseDao.doUpdateObj(TKqwc.class,id,obj);
	}
}
